#include "StdAfx.h"
#include "Bus.h"

using namespace Database;

BusRow::BusRow(void)
{
}

BusRow::~BusRow(void)
{
}

std::string BusRow::getID() const
{
	return _id;
}

void BusRow::setID(const std::string &id)
{
	_id = id;
}

std::string BusRow::getLicencePlate() const
{
	return _licencePlate;
}

void BusRow::setLicencePlate(const std::string &v)
{
	_licencePlate = v;
}

BusTable::BusTable()
{
}

BusTable::~BusTable()
{
}

bool BusTable::insert(const BusRow &br)
{
	MysqlExecute me;
	me.init(getLogin());

	std::string sql = "insert into card set "
		" id='" + br.getID() + "', "
		" licence_plate='" + br.getLicencePlate() + "'";

	return me.execute(sql);
}

bool BusTable::select(std::list<BusRow> &brs)
{
	if (getLogin() == NULL)
	{
		return false;
	}

	std::string sql = "select id, licence_plate"
		" from bus";

	MysqlExecute me;
	me.init(getLogin());

	if (!me.execute(sql))
	{
		return false;
	}

	MysqlResult mr;
	char **v = mr.next(me.result());

	while (v != NULL)
	{
		BusRow br;

		int i = 0;
		br.setID( toStr(v[i++]) );
		br.setLicencePlate( toStr(v[i++]) );

		v = mr.next();

		brs.push_back(br);
	}

	return true;
}

bool BusTable::updateByID(const BusRow &br)
{
	if (getLogin() == NULL)
	{
		return false;
	}

	MysqlExecute me;
	me.init(getLogin());

	std::string sql = "Update bus set"
		" licence_plate='" + br.getLicencePlate() + "'"
		" where id='" + br.getID() + "'";

	return me.execute(sql);
}

bool BusTable::deleteByID(const BusRow &br)
{
	if (getLogin() == NULL)
	{
		return false;
	}

	MysqlExecute me;
	me.init(getLogin());
	std::string sql;

	sql = "delete from bus"
		" where id='" + br.getID() + "'";

	return me.execute(sql);
}